<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="../rurple.css" type="text/css" />

<title>Teaching Reeborg to add</title>
</head>
<body>
<h2 class="title">24. Teaching Reeborg to add</h2>

<p>In this lesson, I will guide you in writing a program that will have
Reeborg add two numbers. While we will do this in the usual way, using
numbers written in base 10, we will be able to easily use the program to add
numbers in other bases!</p>

<!--=========================================-->
<hr class="line" />

<h3 class="try">Warmup</h3>

<ol>
<li>
Write a program that has Reeborg adding 3+2 in the following way.<br />
<img alt="3+2" src="../../images/intro/add3_2_start.png" /><img alt="lead to"
src="../../images/lead_to.png" /> <img alt="5" src=
"../../images/intro/add3_2_end.png" /><br />
Write it so that it would also work to add 1+4.
</li>
<li>
Change your program so that Reeborg can add 13+22 in the following way, where
each beeper pile represents a digit.<br />
<img alt="13+22" src="../../images/intro/add13_22_start.png" /><img alt=
"lead to" src="../../images/lead_to.png" /> <img alt="35" src=
"../../images/intro/add13_22_end.png" />
</li>
<li>
Can you write a program so that Reeborg can still add 3+2 <b>but</b> also
so that it can add 8+4 as follows?.<br />
<img alt="8+4" src="../../images/intro/add8_4_start.png" /><img alt=
"lead to" src="../../images/lead_to.png" /> <img alt="12" src=
"../../images/intro/add8_4_end.png" /><br />
Your new program should also add correctly 2+5 and 9+9. Can you see how to do
this? If so, try ... However, if you can not, do not get discouraged;
instead, read on...
</li>
</ol>

<!--=========================================-->
<hr class="line" />

<h3 class="section">Addition review</h3>

<p>Let us add two numbers in the "traditional way", from right to left:</p>

<pre>
  528
+ 634
------
   12  # adding the units first (8+4)
</pre>

<p>We note that we have a "1" to carry over the "tens" column. This "carry
over" is most likely where your program had problems. Let us rewrite it in
the "usual" way and continue.</p>

<pre>
   1
  528
+ 634
------
 1162  
</pre>

<p>Ok, that was a bit brief, but I'm sure you were able to follow. In
Reeborg's world, we would like this addition to look as follows:</p>

<p><img alt="adding start" src="../../images/intro/add_start.png" /><img alt=
"lead to" src="../../images/lead_to.png" /> <img alt="adding end" src=
"../../images/intro/add_end.png" /></p>

<p>Let's tackle first the simpler problem of adding 8+4.</p>

<!--=========================================-->
<hr class="line" />

<h3 class="section">Adding 8+4 in base 10</h3>

<p><span class="warning">Warning:</span> The following exercise may be a bit
difficult and is presented more as a challenge. Read the rest of this lesson
and decide for yourself if you want to try it or proceed directly to learning
some more about Python.</p>

<p>As we have mentioned, the problem of adding numbers so that each beeper
pile represents a single digit comes when we have to add two digits whose
sum is greater than 9 (in base 10). Somehow, we need to keep track of this
magic number (10), no matter what two numbers we are going to add. I have
created a world (file: adding_world.wld) that is big enough to add two
7-digit numbers in base 10 (or even in base 16!). Load up that world file and
I will guide you so that you can write a program that can do additions
properly.</p>

<p>After loading the world file, if you look at the bottom of the screen, you
will see that Reeborg carries 8 beepers. Write a program so that Reeborg puts
a line of beepers, one at each corner of 10<sup>th</sup> street, as
illustrated below (after my program ended, I used the cursor keys to move
Reeborg out of the way as he was standing on top of the beeper in the last
column; this is why you don't see him in the picture below.)</p>

<p><img alt="line of beepers accross 10th street" src=
"../../images/intro/accross10.png" /></p>

<p>Now, make sure you save this program before going any further.<br />
<br />
Reload the world file (so that the world is empty again and Reeborg is
standing at the origin [corner of 1<sup>st</sup> avenue and 1<sup>st</sup>
street]) and add beepers in the bottom right corners so that the display
looks like the following:</p>

<p><img alt="8+4" src="../../images/intro/add8_4_newstart.png" /></p>

<p>Have Reeborg do the following:</p>

<ol>
<li>
Put a line of beepers across 10<sup>th</sup> street as you have done
before.
</li>
<li>
Go to the bottom right of the screen, collecting the two piles (8 and 4)
of beepers.
</li>
<li>
Spread those 12 beepers on a vertical column as illustrated below.
</li>
</ol>

<p><img alt="8+4 spread vertically" src=
"../../images/intro/vertical12.png" /></p>

<p>Now, we have two beepers above the horizontal line of beepers (the units
in the number 12) and an extra beeper on the horizontal line (which we can
use as the "carry over"). So, all that you have to do is</p>

<ol>
<li>
Have Reeborg pick up the 9 beepers below the horizontal line of beepers,
and discard them (perhaps by putting them all on the horizontal line); you
can do this by repeating the following steps:
<ol>
<li>
pick up a beeper
</li>
<li>
check to see if there is another beeper to pick up; if not
</li>
<li>
move ... until you reach a corner where there is another beeper to pick
up ...
</li>
</ol>
at which point you will have reached the horizontal line of beepers. You can
drop them all at that corner.
</li>
<li>
Have Reeborg keep going north at least past the last beeper, as
illustrated below:<br />
<br />
<img alt="8+4" src="../../images/intro/add8_4_middle.png" />
</li>
<li>
Have Reeborg turn around, pick up <b>one</b> beeper and move, repeat
until Reeborg reaches a corner where there is no more beeper to pick up
(below the horizontal line); at this point, Reeborg should be carrying three
beepers.
</li>
<li>
Have Reeborg carry these beepers down, until Reeborg reaches the
wall;
</li>
<li>
Have Reeborg put them all down (3), pick one up (the carry over), move
west and put the carry over beeper down.
</li>
</ol>

<p>All that is left to do is to move Reeborg out of the way to display the
result!</p>

<p><img alt="8+4" src="../../images/intro/add8_4_end.png" /></p>

<p>Well ... actually, those five steps as I wrote them will require writing a
fair bit of code and you might find it a bit difficult to get it right. But
you will if you proceed systematically. Try it out!</p>

<!--=========================================-->
<hr class="line" />

<h3 class="suggested">Adding 3+5</h3>

<p>So, you finally got your program to calculate 8+4. Great! Now try it on
3+5. Does it work? Chances are, it doesn't ... as this doesn't require a
carry over. Can you think of a way to do it? Perhaps you can using only what
we have learn so far... For the solution <i>I</i> wrote, I needed to use a Python keyword we haven't seen yet:
<span class="pykeyword">break</span>. </p>

<!--=========================================-->
<hr class="line" />

<h3 class="suggested">Final challenge</h3>

<p>If you know what it means to add numbers in bases other than base 10, try
to modify your program (or the world file?...) so that you can add numbers in
a different base.</p>

<!--=========================================-->
<hr class="line" />

<h3 class="section">What next?</h3>

<p>So far, within Reeborg's world, we have seen the following Python
keywords:<br />
<span class="pykeyword">def, elif, else, if, not, pass, while</span>. We have
ended on writing a rather complicated program so that Reeborg could add two
numbers. It is time to leave Reeborg's world to see how we can add two
numbers much more easily with "pure" Python. Don't worry: we will come back
to Reeborg's world from time to time, and see how our increased knowledge of
Python can help Reeborg.</p>

<div class="lessons_nav">
<a href="23-trash.htm"><img alt="previous" src=
"../../images/previous.png" />After the storm</a> - <a href=
"../lessons_toc.htm"><img alt="home" src="../../images/home.png" /></a> - <a href=
"../inter/25-interpreter.htm">Python already knows how to add <img alt="next"
src="../../images/next.png" /></a>
</div>
</body>
</html>
